import {
  App
} from './App';
import {
  Header
} from '../components/header';
import {
  Carousel
} from '../components/carousel';
import {
  BoardTitle
} from '../components/board_title';
import {
  ShowBoard
} from '../components/show_board';
import {
  Footer
} from '../components/footer';

class Index extends App {
  constructor($, app) {
    super($, app, {
      swiper: true,
      phone: true,
      field: true
    });
  }

  render() {
    new Header(this.$app, this.cache.fieldDatas, this.cache.phoneDatas).init();

    // console.log(new Header(this.$app));
    /**
     * Header{
     *   $el: S.fn.init [div#app],
     *   logo: Logo {name: 'logo', tpl: ƒ},
     *   name: "header"
     * }
     */

    new Carousel(this.$app, this.cache.swiperDatas).init();

    new BoardTitle(this.$app, '手机上新').init();
    new ShowBoard(this.$app, this.fieldDatas('new')).init();
    new BoardTitle(this.$app, '超值手机').init();
    new ShowBoard(this.$app, this.fieldDatas('valuable')).init();
    new BoardTitle(this.$app, '官方推荐').init();
    new ShowBoard(this.$app, this.fieldDatas('recom')).init();
    new Footer(this.$app).init();




    //插入body最前面
    $('body').prepend(this.$app);
  }

  fieldDatas(field) {
    /**
     console.log(this.phoneDatas);
     * (21)[
     *   {
     *     buy_num_limit: "3",
     *     color: "[\"全息幻彩蓝\",\"深空灰\",\"全息幻彩紫\"]",
     *     default_price: "1999",
     *     field: "se",
     *     id: "1",
     *     most_value: "0",
     *     new: "1",
     *     phone_name: "小米9 SE",
     *     pics: "[{\"0\":[\"https://i8.mifile.cn/a1/pms_1550572195.46364290.jpg\",...],
     *     recom: "0",
     *     slogan: "「6GB+128GB，优惠300元，仅需1999元」索尼4800万超广角三摄 / 骁龙712全球首发 / 全息幻彩机身 /...],
     *     status: "1",
     *     version_info: "[{\"version\":\"6GB+128GB\",\"price\":\"1999\"},...]"
     *   },
     *   ...
     * ]
     */

    return this.cache.phoneDatas.filter((item, idx) => {
      switch (field) {
        case 'recom':
          return item.recom == 1;
          break;
        case 'new':
          return item.new == 1;
          break;
        case 'valuable':
          return item.most_value == 1;
          break;
        default:
          break;
      }
    })
  }

}

new Index(jQuery);